package leetcode.weekly.week303;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.helper.H;

public class FoodRatings {

	String[] foods;
	String[] cuisines;
	int[] ratings;
	Map<String, Integer> map;
	int n;
	Map<String, int[]> cs;
	Map<String, List<Integer>> hash;

	public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
		this.foods = foods;
		this.cuisines = cuisines;
		this.ratings = ratings;
		this.map = new HashMap<>();
		this.n = foods.length;
		for (int i = 0; i < this.n; i++) {
			map.put(foods[i], i);
		}
		this.cs = new HashMap<>();
		this.hash = new HashMap<>();
		for (int i = 0; i < this.n; i++) {
			String cuisine = cuisines[i];
			if (cs.containsKey(cuisine)) {
				int[] max = cs.get(cuisine);
				if (this.ratings[i] > max[0]) {
					cs.put(cuisine, new int[] { this.ratings[i], i });
				} else if (this.ratings[i] == max[0] && this.foods[i].compareTo(this.foods[max[1]]) <= 0) {
					cs.put(cuisine, new int[] { this.ratings[i], i });
				}
				List<Integer> list = hash.get(cuisine);
				list.add(i);
				hash.put(cuisine, list);
			} else {
				cs.put(cuisine, new int[] { this.ratings[i], i });
				List<Integer> list = new ArrayList<>();
				list.add(i);
				hash.put(cuisine, list);
			}

		}

	}

	public void changeRating(String food, int newRating) {
		int i = map.get(food);
		this.ratings[i] = newRating;
		String cuisine = this.cuisines[i];
		int[] max = cs.get(cuisine);
		if (i == max[1]) {
			if (newRating > max[0]) {
				cs.put(cuisine, new int[] { this.ratings[i], i });
			} else if (newRating < max[0]) {
				List<Integer> list = hash.get(cuisine);
				int ans = newRating;
				String ansName = this.foods[max[1]];
				int ansi = max[1];
				for (int index : list) {
					int rate = this.ratings[index];
					String curName = this.foods[index];
					if (rate > ans || (rate == ans && curName.compareTo(ansName) < 0)) {
						ans = rate;
						ansName = curName;
						ansi = index;
					}
				}
				cs.put(cuisine, new int[] { ans, ansi });
			}
		} else if (this.ratings[i] > max[0]) {
			cs.put(cuisine, new int[] { this.ratings[i], i });
		} else if (this.ratings[i] == max[0] && this.foods[i].compareTo(this.foods[max[1]]) <= 0) {
			cs.put(cuisine, new int[] { this.ratings[i], i });
		}
	}

	public String highestRated(String cuisine) {
		return this.foods[cs.get(cuisine)[1]];
	}

	public static void main(String[] args) {
		FoodRatings foodRatings = new FoodRatings(
				H.str2array2("[\"emgqdbo\",\"jmvfxjohq\",\"qnvseohnoe\",\"yhptazyko\",\"ocqmvmwjq\"]"),
				H.str2array2("[\"snaxol\",\"snaxol\",\"snaxol\",\"fajbervsj\",\"fajbervsj\"]"),
				H.str2array3("[2,6,18,6,5]"));
		foodRatings.changeRating("qnvseohnoe", 11);
		foodRatings.changeRating("emgqdbo", 3);
//		foodRatings.changeRating("jmvfxjohq", 9);
		foodRatings.changeRating("emgqdbo", 14);
		System.out.println(foodRatings.highestRated("snaxol"));
//		foodRatings.highestRated("korean"); // 返回 "kimchi"
//											// "kimchi" 是分数最高的韩式料理，评分为 9 。
//		foodRatings.highestRated("japanese"); // 返回 "ramen"
//												// "ramen" 是分数最高的日式料理，评分为 14 。
//		foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
//		foodRatings.highestRated("japanese"); // 返回 "sushi"
//												// "sushi" 是分数最高的日式料理，评分为 16 。
//		foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
//		foodRatings.highestRated("japanese"); // 返回 "ramen"
	}

//	List<Integer> list = cs.get(cuisine);
//	int ans = this.ratings[list.get(0)];
//	String ansName = this.foods[list.get(0)];
//	for (int i = 1; i < list.size(); i++) {
//		int rate = this.ratings[list.get(i)];
//		String curName = this.foods[list.get(i)];
//		if (rate > ans || (rate == ans && curName.compareTo(ansName) < 0)) {
//			ans = rate;
//			ansName = curName;
//		}
//	}
}
